/*
 * Copyright 2011 Samuel Karp, Alex Wong
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */
#include "resources.h"
#include "globals.h"
#include "main_dialog.h"
#include "dxdiagcom.h"
#include "displaydialog.h"
#include "utility.h"
#include "d3dx9test.h"
#include <windows.h>
#include <assert.h>
#include <commctrl.h>
#include <stdio.h>
#include <wchar.h>
#include <winuser.h>


VOID PopulateDisplayTabLabels(HWND hDisplayTabDialog);
VOID PopulateDisplayTabValues(HWND hDisplayTabDialog);
BOOL CALLBACK DisplayTabDialogProc(HWND hwnd, UINT message, WPARAM wParam,
                                   LPARAM lParam)
{
    switch (message) {
    case WM_INITDIALOG:
        return TRUE;
    case WM_COMMAND:
        switch (LOWORD(wParam)) {
        case DISPLAYTAB_D3DTEST:
            D3DTest_testD3D();
            break;
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        return TRUE;
    case WM_CLOSE:
        DestroyWindow(hwnd);
        return TRUE;
    }
    return FALSE;
}

/* placeholder */
VOID ResizeDisplayTab(HWND hSoundTab, RECT tabDisplayArea)
{
}

VOID CreateDisplayTabDialog(Tab * tab,
                            DxDiag_DisplayDevice * displayDevice)
{
    HWND hDisplayTabDialog;

    /* Create display dialog */
    hDisplayTabDialog =
        CreateDialogW(g_hinstance, MAKEINTRESOURCE(DISPLAYTAB_DLG),
                      g_hmaindialog, DisplayTabDialogProc);
    tab_initialize(tab, hDisplayTabDialog, ResizeDisplayTab);
    SetWindowLongW(hDisplayTabDialog, GWL_USERDATA, (LONG) displayDevice);
    if (hDisplayTabDialog == NULL) {
        MessageBoxW(g_hmaindialog, L"Display tab could not be created",
                    L"Fatal error", MB_OK | MB_ICONINFORMATION);
    }
    PopulateDisplayTabLabels(hDisplayTabDialog);
    PopulateDisplayTabValues(hDisplayTabDialog);
}

VOID PopulateDisplayTabValues(HWND hDisplayTabDialog)
{
    DxDiag_DisplayDevice *displayDevice;

    displayDevice =
        (DxDiag_DisplayDevice *) GetWindowLongW(hDisplayTabDialog,
                                                GWL_USERDATA);

    set_control_text(hDisplayTabDialog,
                     (WCHAR *) displayDevice->szDeviceName,
                     DISPLAYTAB_NAMEDATA);
    set_control_text(hDisplayTabDialog,
                     (WCHAR *) displayDevice->szManufacturer,
                     DISPLAYTAB_MFGDATA);
    set_control_text(hDisplayTabDialog,
                     (WCHAR *) displayDevice->szChipType,
                     DISPLAYTAB_CHIPDATA);
    set_control_text(hDisplayTabDialog, (WCHAR *) displayDevice->szDACType,
                     DISPLAYTAB_DACDATA);
    set_control_text(hDisplayTabDialog,
                     (WCHAR *) displayDevice->szDisplayMemoryLocalized,
                     DISPLAYTAB_MEMDATA);
    set_control_text(hDisplayTabDialog,
                     (WCHAR *) displayDevice->szDisplayModeLocalized,
                     DISPLAYTAB_MODEDATA);
    set_control_text(hDisplayTabDialog,
                     (WCHAR *) displayDevice->szMonitorName,
                     DISPLAYTAB_MONITORDATA);

    set_control_text(hDisplayTabDialog,
                     (WCHAR *) displayDevice->szDriverName,
                     DISPLAYTAB_DRIVERDATA);
    set_control_text(hDisplayTabDialog,
                     (WCHAR *) displayDevice->szDriverVersion,
                     DISPLAYTAB_VERSIONDATA);
    set_control_text(hDisplayTabDialog,
                     (WCHAR *) displayDevice->szDriverDateLocalized,
                     DISPLAYTAB_DATEDATA);
    set_control_text(hDisplayTabDialog,
                     (WCHAR *) displayDevice->dwWHQLLevel,
                     DISPLAYTAB_WHQLDATA);
    set_control_text(hDisplayTabDialog, (WCHAR *) displayDevice->szMiniVdd,
                     DISPLAYTAB_MINIDATA);
    set_control_text(hDisplayTabDialog, (WCHAR *) displayDevice->szVdd,
                     DISPLAYTAB_VDDDATA);
    set_control_text(hDisplayTabDialog,
                     (WCHAR *) displayDevice->szDDIVersionLocalized,
                     DISPLAYTAB_DDIDATA);
}

VOID PopulateDisplayTabLabels(HWND hDisplayTabDialog)
{
    /* Device group */
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_DEVICE,
                              DISPLAYTAB_DEVICE);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_NAMELABEL,
                              DISPLAYTAB_NAMELABEL);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_MFGLABEL,
                              DISPLAYTAB_MFGLABEL);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_CHIPLABEL,
                              DISPLAYTAB_CHIPLABEL);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_DACLABEL,
                              DISPLAYTAB_DACLABEL);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_MEMLABEL,
                              DISPLAYTAB_MEMLABEL);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_MODELABEL,
                              DISPLAYTAB_MODELABEL);
    set_control_text_resource(hDisplayTabDialog,
                              STRING_DLG_DSP_MONITORLABEL,
                              DISPLAYTAB_MONITORLABEL);

    /* Drivers group */
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_DRIVERS,
                              DISPLAYTAB_DRIVERS);
    set_control_text_resource(hDisplayTabDialog,
                              STRING_DLG_DSP_DRIVERLABEL,
                              DISPLAYTAB_DRIVERLABEL);
    set_control_text_resource(hDisplayTabDialog,
                              STRING_DLG_DSP_VERSIONLABEL,
                              DISPLAYTAB_VERSIONLABEL);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_DATELABEL,
                              DISPLAYTAB_DATELABEL);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_WHQLLABEL,
                              DISPLAYTAB_WHQLLABEL);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_MINILABEL,
                              DISPLAYTAB_MINILABEL);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_VDDLABEL,
                              DISPLAYTAB_VDDLABEL);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_DDILABEL,
                              DISPLAYTAB_DDILABEL);

    /* Features group */
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_FEATURES,
                              DISPLAYTAB_FEATURES);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_DDRAWLABEL,
                              DISPLAYTAB_DDRAWLABEL);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_D3DLABEL,
                              DISPLAYTAB_D3DLABEL);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_AGPLABEL,
                              DISPLAYTAB_AGPLABEL);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_DDRAWBTN,
                              DISPLAYTAB_DDRAWBTN);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_D3DBTN,
                              DISPLAYTAB_D3DBTN);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_AGPBTN,
                              DISPLAYTAB_AGPBTN);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_DDRAWTEST,
                              DISPLAYTAB_DDRAWTEST);
    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_D3DTEST,
                              DISPLAYTAB_D3DTEST);

    set_control_text_resource(hDisplayTabDialog, STRING_DLG_DSP_NOTES,
                              DISPLAYTAB_NOTES);
}
